//
//  BindAccountView.swift
//  ShouChong
//
//  Created by yw on 2024/9/20.
//

import SwiftUI

struct BindAccountView: View {
    @Environment(\.presentationMode) var mode
    @State private var account:String = ""
    @State private var password:String = ""
    @ObservedObject var vm:UserViewModel = UserViewModel()
    var body: some View {
        ScrollView {
            HStack {
                Spacer()
                ShakeButton(label:
                                Image(systemName: "xmark")
                    .foregroundColor(Color("color_gray"))
                            
                ) {
                    mode.wrappedValue.dismiss()
                }.modifier(MainTextModifier(fontWeight: .bold))
                    .padding(.horizontal,20)
                    .padding(.vertical,10)
            }
            .padding(.top,10)
            .padding(.bottom,20)
            if vm.model.account.count == 0 || vm.model.account == "" {
                bindView
            } else {
                notBindView
            }
            
        }
        .background(Color("color_bg"))
    }
    
    
    var notBindView:some View {
        VStack {
            Text("您的账号是：123123")
                .modifier(MainTextModifier(fontSize: 18, fontWeight: .bold, foreColor: Color("color_text_black")))
        }
    }
    
    var bindView:some View {
        VStack {
            HStack {
                Text("输入您绑定的账号，一旦绑定后账号无法修改⚠️")
                    .modifier(MainTextModifier(fontSize: 12, fontWeight: .regular, foreColor: Color("color_gray")))
                Spacer()
            }
            .padding(.horizontal,10)
            .padding(.bottom,4)
            
            TextField("建议输入QQ号/邮箱", text: $account)
                .padding(.horizontal,10)
                .frame(width: kScreenWidth - 20,height: 56)
                .background(Color("color_white"))
                .cornerRadius(10)
                .padding(.bottom,20)
                    
            
            
            HStack {
                Text("输入您账号的密码，长度不少于6位")
                    .modifier(MainTextModifier(fontSize: 12, fontWeight: .regular, foreColor: Color("color_gray")))
                Spacer()
            }
            .padding(.horizontal,10)
            .padding(.bottom,4)
            
            SecureField("请输入密码", text: $password)
                .padding(.horizontal,10)
                .frame(width: kScreenWidth - 20,height: 56)
                .background(Color("color_white"))
                .cornerRadius(10)
                .padding(.bottom,40)
            
            ShakeButton(label: Text("确 定").frame(width: kScreenWidth - 50)) {
                UIApplication.shared.endEditing()
                if account.count < 6 {
                    YWToast.show(text: "账号长度过短，建议6位以上")
                    return
                }
                if password.count < 6 {
                    YWToast.show(text: "密码长度过短，建议6位以上")
                    return
                }
                
                vm.bindUserMsg(account: account, password: password) {
                    DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
                        mode.wrappedValue.dismiss()
                    }
                }
            }.modifier(MainButtonModifier(width: kScreenWidth - 50))
                
        }
        
        
    }
}


